package com.fhd.servlet;

import com.fhd.constant.Constants;
import com.fhd.pojo.Bill;
import com.fhd.pojo.Client;
import com.fhd.pojo.House;
import com.fhd.pojo.Merchant;
import com.fhd.service.Bill.BillService;
import com.fhd.service.Bill.BillServiceImplement;
import com.fhd.service.House.HouseService;
import com.fhd.service.House.HouseServiceImplement;
import com.fhd.service.Merchant.MerchantService;
import com.fhd.service.Merchant.MerchantServiceImplement;
import com.fhd.utils.DateTimeUtil;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Objects;

public class MerchantServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if (Objects.equals(method,"updateMerchantInformation")) {
            updateMerchantInformation(req, resp);
        } else if (Objects.equals(method,"merchantLogout")) {
            merchantLogout(req, resp);
        } else if (Objects.equals(method,"getMerchantHouse")) {
            getMerchantHouse(req, resp);
        } else if (Objects.equals(method,"addHouse")) {
            addHouse(req, resp);
        } else if (Objects.equals(method,"deleteHouse")) {
            deleteHouse(req, resp);
        } else if (Objects.equals(method,"updateHouse")) {
            updateHouse(req,resp);
        } else if (Objects.equals(method,"getMerchantBill")) {
            getMerchantBill(req,resp);
        } else if (Objects.equals(method,"merchantCancelBill")) {
            try {
                merchantCancelBill(req,resp);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        } else if (Objects.equals(method,"reGetInformation")) {
            reGetInformation(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    // 更改商户信息
    public void updateMerchantInformation(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 确认身份
        Object object = req.getSession().getAttribute(Constants.MERCHANT_SESSION);
        if (object != null) {
            Merchant merchant = (Merchant) object;
            int id = merchant.getId();
            // 获取参数
            String userCode = req.getParameter("userCode");
            String userPassword = req.getParameter("userPassword");
            String userName = req.getParameter("userName");
            String shopName = req.getParameter("shopName");
            String address = req.getParameter("address");
            String phone = req.getParameter("phone");
            String keyWord = req.getParameter("keyWord");

            // String转成Time
            String parameter1 = req.getParameter("workTimeBegin");
            String parameter2 = req.getParameter("workTimeEnd");

            Time workTimeBegin = null;
            Time workTimeEnd = null;
            if (!StringUtils.isNullOrEmpty(parameter1)) {
                workTimeBegin = DateTimeUtil.stringToTime(parameter1);
            }
            if (!StringUtils.isNullOrEmpty(parameter2)) {
                workTimeEnd = DateTimeUtil.stringToTime(parameter2);
            }

            String parameter = req.getParameter("openSituation");
            int opensituation;
            if (Objects.equals(parameter,"0")) {
                opensituation = 0;
            } else {
                opensituation = 1;
            }

            // 调用业务层
            MerchantService merchantService = new MerchantServiceImplement();
            int row = merchantService.updateInformation(id, userCode, userPassword, userName, shopName, address, phone, keyWord, workTimeBegin, workTimeEnd, opensituation);
            if (row > 0) {
                // 更改成功,回到登录页
                req.getSession().setAttribute(Constants.UPDATE_MERCHANT_MESSAGE,"修改成功，请重新登录");
                resp.sendRedirect("login.jsp");
            } else {
                // 更改失败
                req.getSession().setAttribute(Constants.UPDATE_MERCHANT_MESSAGE,"修改失败，请重试");
                resp.sendRedirect("jsp/merchantUpdateInformation.jsp");
            }

        }
    }

    // 商户退出登录
    public void merchantLogout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.getSession().removeAttribute(Constants.MERCHANT_SESSION);
        resp.sendRedirect("login.jsp");
    }

    // 获得该商户所有房型
    public void getMerchantHouse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object object = req.getSession().getAttribute(Constants.MERCHANT_SESSION);
        if (object != null) {
            Merchant merchant = (Merchant) object;
            // 获取参数
            int id = merchant.getId();
            // 调用业务层
            HouseService houseService = new HouseServiceImplement();
            ArrayList<House> houses = houseService.getSomeoneHouse(id);
            // 判断业务结果
            if (houses != null && !houses.isEmpty()) {
                // 找得到房型
                req.getSession().setAttribute(Constants.GET_MERCHANT_HOUSE_SESSION,houses);
            } else {
                // 找不到
                req.getSession().setAttribute(Constants.GET_MERCHANT_HOUSE_MESSAGE,"该商户暂且没有房型，请添加先噢~");
            }
            resp.sendRedirect("jsp/merchantHouse.jsp");
        }
    }

    // 增加房型
    public void addHouse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Object object = req.getSession().getAttribute(Constants.MERCHANT_SESSION);
        if (object != null) {
            Merchant merchant = (Merchant) object;
            int merchantId = merchant.getId();
            // 获取前端来的参数
            String houseName = req.getParameter("houseName");
            int price = Integer.parseInt(req.getParameter("price"));
            int remainNumber = Integer.parseInt(req.getParameter("remainNumber"));
            String keyWord = req.getParameter("keyWord");
            // 调用业务层
            HouseService houseService = new HouseServiceImplement();
            int row = houseService.addHouse(merchantId, houseName, price, remainNumber, keyWord);
            // 判断业务结果
            if (row > 0) {
                // 添加成功
                req.getSession().setAttribute(Constants.ADD_HOUSE_MESSAGE,"添加成功");
            } else {
                // 添加失败
                req.getSession().setAttribute(Constants.ADD_HOUSE_MESSAGE,"添加失败，请重试");
            }
            resp.sendRedirect("merchant.do?method=getMerchantHouse");
        }
    }


    // 删除房型
    public void deleteHouse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取前端参数
        int id = Integer.parseInt(req.getParameter("id"));
        // 调用业务层
        HouseService houseService = new HouseServiceImplement();
        int row = houseService.deleteHouse(id);
        // 判断业务结果
        if (row > 0) {
            // 删除成功
            req.getSession().setAttribute(Constants.DELETE_HOUSE_MESSAGE,"删除成功");
        } else {
            // 删除失败
            req.getSession().setAttribute(Constants.DELETE_HOUSE_MESSAGE,"删除失败");
        }
        resp.sendRedirect("merchant.do?method=getMerchantHouse");
    }

    // 修改房型信息
    public void updateHouse(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 从前端获取参数
        int id = Integer.parseInt(req.getParameter("id"));
        String houseName = req.getParameter("houseName");
        String price = req.getParameter("price");
        String remainNumber = req.getParameter("remainNumber");
        String keyWord = req.getParameter("keyWord");

        if(StringUtils.isNullOrEmpty(houseName) && StringUtils.isNullOrEmpty(price) && StringUtils.isNullOrEmpty(remainNumber) && StringUtils.isNullOrEmpty(keyWord)) {
            // 什么都没传，返回提示信息
            req.getSession().setAttribute(Constants.UPDATE_HOUSE_MESSAGE,"请至少填写一样信息");
            resp.sendRedirect("jsp/merchantUpdateHouse.jsp");
        } else {
            int price1 = -1;
            if (!StringUtils.isNullOrEmpty(price)) {
                price1 = Integer.parseInt(price);
            }
            int remainNumber1 = -1;
            if (!StringUtils.isNullOrEmpty(remainNumber)) {
                remainNumber1 = Integer.parseInt(remainNumber);
            }
            // 调用业务层
            HouseService houseService = new HouseServiceImplement();
            int row = houseService.updateHouse(id, houseName, price1, remainNumber1, keyWord);

            // 判断业务结果
            if (row > 0) {
                // 修改成功
                req.getSession().setAttribute(Constants.UPDATE_HOUSE_MESSAGE,"修改成功");
            } else {
                // 修改失败
                req.getSession().setAttribute(Constants.UPDATE_HOUSE_MESSAGE,"修改失败");

            }
            resp.sendRedirect("merchant.do?method=getMerchantHouse");
        }
    }

    // 获取该商户的所有订单
    public void getMerchantBill(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Object object = req.getSession().getAttribute(Constants.MERCHANT_SESSION);
        if (object != null) {
            Merchant merchant = (Merchant) object;
            int id = merchant.getId();

            // 调用业务层
            BillService billService = new BillServiceImplement();
            ArrayList<Bill> bills = billService.getMerchantBill(id);

            if (bills.isEmpty()) {
                // 传信息
                req.getSession().setAttribute(Constants.MERCHANT_BILL_MESSAGE,"暂且没有订单数据");
            } else {
                // 放进Session中，跳转页面
                req.getSession().setAttribute(Constants.MERCHANT_BILL_SESSION,bills);
            }
            resp.sendRedirect("jsp/merchantBill.jsp");
        }
    }

    // 商户取消订单
    public void merchantCancelBill(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        // 从前端获取参数
        int billId = Integer.parseInt(req.getParameter("billId"));
        int clientId = Integer.parseInt(req.getParameter("clientId"));
        int merchantId = Integer.parseInt(req.getParameter("merchantId"));
        int houseId = Integer.parseInt(req.getParameter("houseId"));
        int money = Integer.parseInt(req.getParameter("money"));
        int number = Integer.parseInt(req.getParameter("number"));

        String parameter = req.getParameter("situation");

        if (Objects.equals(parameter,"取消订单")) {
            int situation = 3;
            // 调用业务层
            BillService billService = new BillServiceImplement();
            int row = billService.cancelBill(billId,clientId,merchantId,houseId,money,number,situation);
            if (row == 5) {
                // 取消成功
                req.getSession().setAttribute(Constants.MERCHANT_CANCEL_BILL_MESSAGE,"取消成功");
            } else {
                // 取消失败
                req.getSession().setAttribute(Constants.MERCHANT_CANCEL_BILL_MESSAGE,"取消失败");
            }
        } else if (Objects.equals(parameter,"客户已入住")) {
            // 确认订单，只需更改订单状态即可
            int situation = 2;
            // 调用业务层
            BillService billService = new BillServiceImplement();
            int row = billService.updateBillSituation(billId, situation);
            if (row == 1) {
                // 确认成功
                req.getSession().setAttribute(Constants.MERCHANT_CONFIRM_BILL_MESSAGE,"确认成功");
            } else {
                // 确认失败
                req.getSession().setAttribute(Constants.MERCHANT_CONFIRM_BILL_MESSAGE,"确认成功");
            }
        }
        resp.sendRedirect("merchant.do?method=getMerchantBill");
    }

    // 每次进入商户“我的信息”的时候要重新获取一下商户信息
    public void reGetInformation(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int id = ((Merchant) req.getSession().getAttribute(Constants.MERCHANT_SESSION)).getId();
        // 调用业务层
        MerchantService merchantService = new MerchantServiceImplement();
        Merchant merchant = merchantService.getMerchantById(id);
        // 跳转页面
        req.getSession().setAttribute(Constants.MERCHANT_SESSION,merchant);
        resp.sendRedirect("jsp/merchantInformation.jsp");
    }
}


